package com.example.zhangxin.myhuatuob2b.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.activity.GoodsDetailsActivity;
import com.example.zhangxin.myhuatuob2b.activity.SubjectWebActivity;
import com.example.zhangxin.myhuatuob2b.bean.AdvertList;
import com.example.zhangxin.myhuatuob2b.utils.URL;

import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by ZhangXin on 2016/10/11.
 */

public class HeadImageAdapter extends PagerAdapter {

    private Context context;
    private ArrayList<AdvertList> data;

    public HeadImageAdapter(Context context, ArrayList<AdvertList> data) {
        this.context = context;
        this.data = data;
    }


    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        AdvertList advertList = data.get(position%data.size());
        View view = View.inflate(context, R.layout.item_headimage,null);
        ImageView iv_headimage = (ImageView) view.findViewById(R.id.iv_headimage);

        x.image().bind(iv_headimage,advertList.getImage());
        container.addView(view);
        OnImageViewClick(view,advertList.getType(),advertList.getData());
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void  OnImageViewClick(View view, final String type, final String data){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type.equals("keyword")){

                }else if(type.equals("special")){
                    Intent intent = new Intent(context, SubjectWebActivity.class);
                    intent.putExtra("data", URL.URL_SPECIAL + "&special_id=" + data + "&type=html");
                    context.startActivity(intent);
                }else if(type.equals("goods")){
                    Intent intent = new Intent(context, GoodsDetailsActivity.class);
                    intent.putExtra("goods_id", data);
                    context.startActivity(intent);
                }else if(type.equals("url")){
                    Intent intent = new Intent(context, SubjectWebActivity.class);
                    intent.putExtra("data", data);
                    context.startActivity(intent);
                }
            }
        });
    }
}
